<template>
  <div class="main">
    <el-row style="margin-bottom:20px">
      <el-col :offset="20">
        <el-button type="primary" icon="el-icon-back" @click.native="back">返回</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-form
        :inline="true"
        :disabled="resouceTableType == 2 ? true : false"
        label-width="120px"
        labelPosition="left"
        :model="addForm"
      >
        <el-row type="flex" justify="space-around">
          <el-col :span="11">
            <el-form-item label="合同号">
              <el-input v-model="addForm.code" placeholder="请输入合同号" style="width:300px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="合同签订日期">
              <el-date-picker
                style="width:300px"
                type="date"
                v-model="addForm.date"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex" justify="space-around">
          <el-col :span="11">
            <el-form-item label="合同名称">
              <el-input v-model="addForm.name" placeholder="请输入合同名称" style="width:300px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="合同类型">
              <el-select v-model="addForm.type" style="width:300px;" placeholder="请选择">
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex" justify="space-around">
          <el-col :span="11">
            <el-form-item label="投放周期">
              <el-date-picker
                style="width:300px"
                type="daterange"
                v-model="addForm.cycle"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item
              label="合同额"
              prop="money"
              :rules="[

                { type: 'number', message: '必须为数字值'}
              ]"
            >
              <el-input
                type="money"
                v-model.number="addForm.money"
                placeholder="请输入合同额"
                style="width:300px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex" justify="space-around">
          <el-col :span="11">
            <el-form-item label="销售人员">
              <el-input v-model="addForm.sales.name" disabled style="width:300px"></el-input>
            </el-form-item>
            <span
              class="chose"
              @click="addSalesInfo"
              v-if="resouceTableType == 2 ? false : true"
            >点击选择</span>
          </el-col>
          <el-col :span="11">
            <el-form-item label="代理商">
              <el-input v-model="addForm.agent.name" disabled style="width:300px"></el-input>
            </el-form-item>
            <span
              class="chose"
              @click="addAgentInfo"
              v-if="resouceTableType == 2 ? false : true"
            >点击选择</span>
          </el-col>
        </el-row>

        <el-row type="flex" justify="space-around">
          <el-col :span="11">
            <el-form-item label="广告主">
              <el-input v-model="addForm.media_up.name" disabled style="width:300px"></el-input>
            </el-form-item>
            <span
              class="chose"
              @click="addAdderInfo"
              v-if="resouceTableType == 2 ? false : true"
            >点击选择</span>
          </el-col>
          <el-col :span="11">
            <el-form-item label="行业">
              <el-select v-model="addForm.industry_id" style="width:300px;" placeholder="请选择">
                <el-option
                  v-for="item in industryList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex" justify="space-around">
          <el-col :span="11">
            <el-form-item label="关联账户">
              <el-select v-model="addForm.contact_num" style="width:300px;" placeholder="请选择">
                <el-option
                  v-for="item in accountList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="品牌">
              <el-select
                v-model="addForm.brand"
                allow-create
                filterable
                multiple
                style="width:300px;"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in brandList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex" justify="space-around">
          <el-col :span="23">
            <el-form-item label="投放要求">
              <el-input
                type="textarea"
                v-model="addForm.note"
                style="width:300px;"
                :rows="2"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <div class="addResouce">
            <span class="controlName">媒体资源</span>
            <span
              class="addControl"
              @click="addResouce"
              v-if="resouceTableType == 2 ? false : true"
            >加入资源</span>
          </div>
        </el-row>
        <el-row>
          <resourceTable
            :tableData="resouceTable"
            :type="resouceTableType"
            @handleResourceChange="handleResourceChange"
            @handleResourceDelete="resourceDelete"
          ></resourceTable>
        </el-row>

        <!-- <el-row>
          <div class="addResouce">
            <span class="controlName">产品</span>
            <span
              class="addControl"
              @click="addResouce"
              v-if="resouceTableType == 2 ? false : true"
            >加入产品</span>
          </div>
        </el-row>
        <el-row>
          <product-table
            :tableData="productChoseTable"

          ></product-table>
        </el-row>-->

        <el-row>
          <div class="addResouce">
            <span class="controlName">合同款项</span>
            <span
              class="addControl"
              @click="addPayPlan"
              v-if="resouceTableType == 2 ? false : true"
            >添加回款计划</span>
            <span
              class="addControl"
              @click="addRefundPlan"
              v-if="resouceTableType == 2 ? false : true"
            >添加退款计划</span>
          </div>
        </el-row>
        <el-row>
          <moneyTableBox
            :tableData="moneyTable"
            @handleMoneyListDelete="handleMoneyListDelete"
            @handleMoneyListChange="handleMoneyListChange"
            :type="resouceTableType"
          ></moneyTableBox>
        </el-row>
        <el-row style="margin-top:20px;margin-left:30px" type="flex" justify="flex-start">
          <el-col :span="11">
            <el-form-item label="相关附件">
              <el-upload
                class="upload-demo"
                action="http://dev.media.xkphp.com/api/upload"
                multiple
                :on-success="contractSuccess"
                :show-file-list="false"
              >
                <el-button
                  size="small"
                  type="primary"
                  v-if="resouceTableType == 2 ? false : true"
                >点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top:20px;margin-left:30px" type="flex" justify="flex-start">
          <div class="fileList" v-if="addForm.file_path.length>0">
            <span>文件列表</span>
            <div class="fileListBox">
              <a v-for="item in addForm.file_path" :key="item.id" :href="item.file_path">{{item.file_name}}</a>
            </div>
          </div>
        </el-row>

        <el-row style="margin-top:20px;margin-left:30px" type="flex" justify="flex-start">
          <el-col :span="11">
            <el-form-item label="状态">
              <el-radio
                v-model="addForm.status"
                label="1"
                :disabled="resouceTableType == 2 ? true : false"
              >草稿</el-radio>
              <el-radio
                v-model="addForm.status"
                label="2"
                :disabled="resouceTableType == 2 ? true : false"
              >提交</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <el-row style="margin-top:20px" type="flex" justify="space-around">
      <el-button type="primary" icon="el-icon-back" @click.native="back">返回</el-button>
      <el-button
        type="danger"
        v-if="resouceTableType == 2 ? false : true"
      >删除</el-button>
      <el-button
        type="primary"
        @click.native="stopContact(5)"
        v-if="resouceTableType == 2 ? false : true && contact_status != 5"
      >终止合同</el-button>
      <el-button
        type="primary"
        @click.native="stopContact(6)"
        v-if="resouceTableType == 2 ? false : true && contact_status != 6 && contact_status != 5"
      >暂停合同</el-button>
      <el-button
        type="primary"
        @click.native="addContract"
        v-if="resouceTableType == 2 ? false : true"
      >提交</el-button>
    </el-row>
    <el-dialog title="添加销售人员" :visible.sync="showSale" center width="50%">
      <choseAdder
        :sales="addForm.sales"
        @saleChoose="saleChoose"
        @saleCancelChoose="saleCancelChoose"
      ></choseAdder>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showSale = false">取 消</el-button>
        <el-button type="primary" @click="showSale = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="添加代理商" :visible.sync="showAgent" append-to-body center width="50%">
      <choseAgent
        :agentId="addForm.sales.id"
        :agent="addForm.agent"
        @agentChoose="agentChoose"
        @agentCancelChoose="agentCancelChoose"
      ></choseAgent>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAgent = false">取 消</el-button>
        <el-button type="primary" @click="showAgent = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="添加广告主" :visible.sync="showAdder" append-to-body center width="50%">
      <choseMediaUp
        :adderId="addForm.sales.id"
        :adder="addForm.media_up"
        @adderChoose="adderChoose"
        @adderCancelChoose="adderCancelChoose"
      ></choseMediaUp>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAdder = false">取 消</el-button>
        <el-button type="primary" @click="showAdder = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="添加资源" :visible.sync="addResouceShow" destroy-on-close center width="60%">
      <add-resource-box ref="boxChild" :addType="addType"></add-resource-box>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addResouceShow = false">取 消</el-button>
        <el-button type="primary" @click="resourceSave">加 入</el-button>
      </span>
    </el-dialog>

    <el-dialog title="添加回款计划" :visible.sync="addMoneyShow" destroy-on-close center width="30%">
      <addMoneyBox ref="moneyChild" :moneyType="moneyType"></addMoneyBox>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addMoneyShow = false">取 消</el-button>
        <el-button type="primary" @click="addMoneySave">加 入</el-button>
      </span>
    </el-dialog>

    <el-dialog title="退款计划" :visible.sync="addRefundShow" center width="30%">
      <addRefundBox ref="refundChild" :refundType="refundType"></addRefundBox>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRefundShow = false">取 消</el-button>
        <el-button type="primary" @click="addRefundSave">加 入</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  Choice_Salesman,
  Choice_Agent,
  Choice_advertisers,
  addvertise_type,
  getmediaINFO,
  getChannel,
  getAccount,
  getBand,
  GET_CONTRACT_BACK_MONEY,
  addContract,
  checkContract,
  editContract,
  stopContact
} from "../../api/Salesman.js";
import { SETDATE } from "../../api/System.js";
// import dataSource from "./motheds.js";
// import addAder from "../market_manage/components/addAder.vue";
// import addAgent from "../market_manage/components/addAgent.vue";
import resourceTable from "./components/resourceTable.vue";
import productTable from "./components/productTable.vue";
import moneyTableBox from "./components/moneyTable.vue";
import addResourceBox from "./components/addResourceBox.vue";
import addMoneyBox from "./components/addMoneyBox.vue";
import addRefundBox from "./components/addRefundBox.vue";
import choseAdder from "./components/choseAdder.vue";
import choseAgent from "./components/choseAgent.vue";
import choseMediaUp from "./components/choseMediaUp.vue";
import formatEveryDay from "./components/timeFormat.js";
import moment from "moment";
import generateUUID from "./components/uuid.js";
import { string } from "clipboard";
export default {
  components: {
    choseAdder,
    choseAgent,
    choseMediaUp,
    resourceTable,
    addResourceBox,
    moneyTableBox,
    addMoneyBox,
    addRefundBox,
    productTable
  },
  props: ["type"],
  data() {
    return {
      // 表单数据
      addForm: {
        code: "",
        date: "",
        name: "",
        type: "",
        cycle: "",
        money: "",
        file_path: [],
        sales: {
          name: "",
          id: ""
        },
        agent: {
          name: "",
          id: ""
        },
        media_up: {
          name: "",
          id: ""
        },
        industry_id: "",
        contact_num: "",
        brand: "",
        note: "",
        status: "1"
      },
      typeList: [
        {
          value: 1,
          label: "商业广告"
        },
        {
          value: 2,
          label: "公益广告"
        }
      ],
      industryList: [],
      accountList: [],
      brandList: [],
      showSale: false,
      showAgent: false,
      showAdder: false,
      addResouceShow: false,
      // table data
      resouceTable: [],
      productChoseTable: [],
      resouceTableType: 1,
      addType: null,
      addMoneyShow: false,
      moneyType: null,
      moneyTable: [],
      addRefundShow: false,
      refundType: null,
      refundList: {},
      contact_status:1
    };
  },
  methods: {
    stopContact(stopType){
      stopContact(this.$route.query.id,{type:stopType}).then(res =>{
        if (res.code == 200){
          this.$message.success("操作成功");
          this.$router.back();
        }
      })
    },
    back() {
      this.$router.back();
    },
    // 数据初始化
    getAccountList() {
      let data = {};
      getAccount(data).then(res => {
        if (res.code == 200) {
          this.accountList = res.data;
          this.accountList.forEach(element => {
            element.created_at = SETDATE(element.created_at, true);
          });
        }
      });
    },
    getBrandList(id) {
      getBand(id).then(res => {
        if (res.code == 200) {
          this.brandList = res.data;

          this.brandList.forEach(element => {
            element.created_at = SETDATE(element.created_at, true);
          });
        }
      });
    },
    getIndustry() {
      let data = {
        type: 1
      };
      addvertise_type(data).then(res => {
        if (res.code == 200) {
          this.industryList = res.data;

          this.industryList.forEach(element => {
            element.created_at = SETDATE(element.created_at, true);
          });
        }
      });
    },
    // 添加事件
    // 添加销售
    addSalesInfo() {
      this.showSale = !this.showSale;
    },
    saleChoose(data) {
      this.addForm.sales.name = data.get_admin_info.name;
      this.addForm.sales.id = data.id;
    },
    saleCancelChoose() {
      this.addForm.sales.name = "";
      this.addForm.sales.id = "";
    },
    addAgentInfo() {
      if (this.addForm.sales.id == "") {
        this.$message.error("请先选择销售人员");
      } else {
        this.showAgent = !this.showAgent;
      }
    },
    agentChoose(data) {
      this.addForm.agent.name = data.name;
      this.addForm.agent.id = data.id;
    },
    agentCancelChoose() {
      this.addForm.agent.name = "";
      this.addForm.agent.id = "";
    },

    addAdderInfo() {
      if (this.addForm.sales.id == "") {
        this.$message.error("请先选择销售人员");
      } else {
        this.showAdder = !this.showAdder;
      }
    },
    adderChoose(data) {
      this.addForm.media_up.name = data.name;
      this.addForm.media_up.id = data.id;
    },
    adderCancelChoose() {
      this.addForm.media_up.name = "";
      this.addForm.media_up.id = "";
    },

    // 加入资源
    addResouce() {
      this.addType = false;
      this.addResouceShow = true;
    },
    resourceSave() {
      if (this.$refs.boxChild.get_resources.expenses == "") {
        this.$message.error("输入费用");
      } else if (this.$refs.boxChild.get_resources.id == "") {
        this.$message.error("请选择资源");
      } else if (this.$refs.boxChild.get_resources.often == "") {
        this.$message.error("请选择时间");
      } else {
        let index = "";
        if (this.resouceTable.length == 0) {
          this.$refs.boxChild.get_resources.fakeID = generateUUID();
          this.resouceTable.push(this.$refs.boxChild.get_resources);
        } else {
          if (this.resouceTableType == 1) {
            if (this.$refs.boxChild.get_resources.fakeID) {
              let index = "";
              this.resouceTable.forEach((p, i) => {
                if (p.fakeID == this.$refs.boxChild.get_resources.fakeID) {
                  this.$set(
                    this.resouceTable,
                    i,
                    this.$refs.boxChild.get_resources
                  );
                }
              });
            } else {
              this.$refs.boxChild.get_resources.fakeID = generateUUID();
              this.resouceTable.push(this.$refs.boxChild.get_resources);
            }
          } else {
            if (this.$refs.boxChild.get_resources.fakeID) {
              let index = "";
              this.resouceTable.forEach((p, i) => {
                if (p.id == this.$refs.boxChild.get_resources.id) {
                  this.$set(
                    this.resouceTable,
                    i,
                    this.$refs.boxChild.get_resources
                  );
                }
              });
            } else {
              this.$refs.boxChild.get_resources.fakeID = generateUUID();
              this.resouceTable.push(this.$refs.boxChild.get_resources);
            }
          }
        }
        this.addResouceShow = false;
      }
    },
    resourceDelete(data) {
      let arr = [data.id];
      let allArr = [...this.resouceTable];
      let finalArr = allArr.filter(item => !arr.includes(item.id));
      this.resouceTable = finalArr;
    },
    handleResourceChange(data) {
      if (this.resouceTableType == 1 || this.resouceTableType == 2) {
        this.addType = { ...data };
      } else if (this.resouceTableType == 3) {
        this.resouceTable.forEach(e => {
          if (e.id == data.id) {
            this.addType = { ...e };
          }
        });
      }

      this.addResouceShow = true;
    },

    addPayPlan() {
      this.addMoneyShow = true;
    },
    handleMoneyListDelete(data) {
      let arr = [data.item_status.id];
      let allArr = [...this.moneyTable];
      let finalArr = allArr.filter(item => !arr.includes(item.item_status.id));
      this.moneyTable = finalArr;
    },
    handleMoneyListChange(data) {
      this.moneyType = { ...data };
      this.addMoneyShow = true;
    },
    addMoneySave() {
      if (this.$refs.moneyChild.pay.payment_time == "") {
        this.$message.error("请选择时间");
      } else if (this.$refs.moneyChild.pay.money == "") {
        this.$message.error("请输入金额");
      } else if (this.$refs.moneyChild.pay.item_status == "") {
        this.$message.error("请选择项目");
      } else {
        if (this.moneyTable.length == 0) {
          this.$refs.moneyChild.pay.fakeID = generateUUID();
          this.moneyTable.push(this.$refs.moneyChild.pay);
        } else {
          if (this.$refs.moneyChild.pay.fakeID) {
            this.moneyTable.forEach((p, i) => {
              if (p.fakeID == this.$refs.moneyChild.pay.fakeID) {
                this.$set(this.moneyTable, i, this.$refs.moneyChild.pay);
              }
            });
          } else {
            this.$refs.moneyChild.pay.fakeID = generateUUID();
            this.moneyTable.push(this.$refs.moneyChild.pay);
          }
        }
        this.addMoneyShow = false;
      }
    },

    addRefundPlan() {
      this.addRefundShow = true;
    },
    addRefundSave() {
      if (this.$refs.refundChild.pay.payment_time == "") {
        this.$message.error("请选择时间");
      } else if (this.$refs.refundChild.pay.money == "") {
        this.$message.error("请输入金额");
      } else if (this.$refs.refundChild.pay.item_status == "") {
        this.$message.error("请选择退款类型");
      } else {
        this.refundList = this.$refs.refundChild.pay;
        this.addRefundShow = false;
      }
    },

    contractSuccess(response, file, fileList) {
      console.log(response);
      this.addForm.file_path.push({
        file_name: file.name,
        file_path: file.response.data.path,
        id: file.response.data.id
      });
      // this.addForm.file_path.push(fileList);
    },
    unique(arr, name) {
      var hash = {};
      return arr.reduce(function(item, next) {
        hash[next[name]] ? "" : (hash[next[name]] = true && item.push(next));
        return item;
      }, []);
    },
    addContract() {
      if (this.addForm.date == "") {
        this.$message.error("请选择签订时间");
      } else if (this.addForm.sales == {}) {
        this.$message.error("请选择销售人员");
      } else if (this.addForm.agent == {}) {
        this.$message.error("请选择代理商");
      } else if (this.addForm.code == "") {
        this.$message.error("请填写合同号");
      } else if (this.addForm.name == "") {
        this.$message.error("请填写合同名称");
      } else if (this.addForm.cycle == []) {
        this.$message.error("请选择投放周期");
      } else if (this.addForm.money == "") {
        this.$message.error("请填写合同额");
      } else if (this.resouceTable.length == 0) {
        this.$message.error("请添加资源");
      } else {
        let data = {
          code: this.addForm.code,
          date: moment(this.addForm.date).unix(),
          sale_id: this.addForm.sales.id,
          name: this.addForm.name,
          agent_id: this.addForm.agent.id,
          industry_id: this.addForm.industry_id,

          media_up_id: this.addForm.media_up.id,
          contact_num: this.addForm.contact_num,
          money: this.addForm.money,
          cycle: JSON.stringify({
            start_time: moment(this.addForm.date[0]).unix(),
            end_time: moment(this.addForm.date[1]).unix()
          }),
          note: this.addForm.note,
          status: this.addForm.status,

          type: this.addForm.type,
          medium: [],
          product: [],
          fund: []
        };
        let brandArr = [];
        let arr1 = [...this.brandList];

        let arr2 = [...this.addForm.brand];

        let arr = arr1.map(item => item.id);
        let postData = [];
        arr2.map(item => {
          let index = arr.indexOf(item);
          console.log(index);
          if (index !== -1) {
            postData.push({
              brand_id: arr1[index].id,
              brand_name: arr1[index].name
            });
          } else {
            postData.push({
              brand_id: "",
              brand_name: item
            });
          }
        });
        data.brand = postData;
        let fileArr = [];
        this.addForm.file_path.forEach(e => {
          fileArr.push(e.id);
        });
        data.file_path = fileArr.join(",");
        this.resouceTable.forEach((e, i) => {
          console.log(e);

          let obj = {
            id: e.id,
            often: e.often,
            second: e.second,
            expenses: e.expenses,
            fees: e.fees,
            remark: e.remark,
            player_date: "",
            gift: e.gift || e.gift == 1 ? 1 : 0
          };
          if (this.resouceTableType == 1) {
            obj.player_date = JSON.stringify({
              type: 1,
              val: formatEveryDay(e.player_date[0], e.player_date[1])
            });
          } else if (this.resouceTableType == 3) {
            obj.player_date = e.player_date;
            obj.id = e.medium_public_id;
          }
          data.medium.push(obj);
        });

        this.moneyTable.forEach((e, i) => {
          let obj = {
            money: e.money,
            item_status: "",
            payment_time: moment(e.payment_time).unix(),
            type: 1
          };
          console.log(e);
          if (this.resouceTableType == 1) {
            obj.item_status = e.item_status_text.id;
          } else if (this.resouceTableType == 3) {
            obj.item_status = e.item_status;
          }
          data.fund.push(obj);
        });
        if (JSON.stringify(this.refundList) != "{}") {
          let e = this.refundList;
          let obj = {
            item_status: e.item_status.id,
            money: e.money,
            payment_time: moment(e.payment_time).unix(),
            file_path: e.file_path.join(","),
            type: 2
          };
          data.fund.push(obj);
        }

        if (this.resouceTableType == 1) {
          addContract(data).then(res => {
            if (res.code == 200) {
              this.$message.success("添加成功");
              this.$router.back();
            }
          });
        } else {
          data.id = this.$route.query.id;
          editContract(data).then(res => {
            if (res.code == 200) {
              this.$message.success("编辑成功");
              this.$router.back();
            }
          });
        }
      }
    },
    getNameById(id, arr) {
      let obj = arr.find(function(x) {
        return x.id == id;
      });
      return obj;
    },
    checkContractFormData() {
      checkContract({
        id: this.$route.query.id
      }).then(res => {
        if (res.code == 200) {
          let data = res.data;
          if (data.status == 5)
            this.contact_status = 5;
          if (data.status ==6)
            this.contact_status = 6;
          data.date = moment.unix(Number(data.date)).format("YYYY-MM-DD");
          data.cycle = [
            moment.unix(JSON.parse(data.cycle).start_time).format("YYYY-MM-DD"),
            moment.unix(JSON.parse(data.cycle).end_time).format("YYYY-MM-DD")
          ];

          this.addForm = { ...data };
          this.addForm.status = String(data.status);

          console.log("fanhui", data.get_resources);
          console.log("fuzhi", this.resouceTable);
          this.resouceTable = data.get_resources;

          this.moneyTable = data.get_contract_fund;
          this.addForm.sales = data.get_sale
            ? data.get_sale
            : {
                id: "",
                name: "未找到该销售人员"
              };
          this.addForm.type = data.type;
          let obrand = [];
          data.get_brands.forEach(e => {
            obrand.push(e.id);
          });
          this.addForm.brand = obrand;
          this.addForm.file_path = [];
          if (data.get_files != [] || data.get_files) {
            this.addForm.file_path = data.get_files;
          }
          this.accountList.forEach(e => {
            if (e.id == data.contact_num) {
              this.addForm.contact_num = e.id;
            }
          });
          // this.addForm.contact_num = Number(data.contact_num);
          this.addForm.media_up = data.get_media_up
            ? data.get_media_up
            : {
                name: "未找到该广告主",
                id: ""
              };
          this.addForm.agent = data.get_agent
            ? data.get_agent
            : {
                id: "",
                name: "未找到该代理商"
              };
        } else {
          this.$message.error("获取合同详情失败");
          this.$router.back();
        }
      });
    }
  },
  computed: {
    oresouceTable() {
      let a = "";
      let data = JSON.stringify(this.resouceTable);
      return (a = JSON.parse(data));
    },
    mediaUpId() {
      return this.addForm.media_up_id;
    }
  },
  watch: {
    mediaUpId(val, oldVal) {
      this.getBrandList(val);
    }
  },
  mounted() {
    // 获取账户
    this.getAccountList();
    // 获取品牌
    // this.getBrandList();
    // 获取行业
    this.getIndustry();
    if (this.$route.query.type == 1) {
      // 新增
      this.resouceTableType = 1;
    } else if (this.$route.query.type == 2) {
      // 查看
      this.resouceTableType = 2;
      this.checkContractFormData();
    } else if (this.$route.query.type == 3) {
      // 编辑
      this.resouceTableType = 3;
      this.checkContractFormData();
    }
  }
};
</script>

<style scoped lang='scss'>
.main {
  width: 100%;
  height: 100%;
}
.chose {
  color: #169bd5;
  margin-left: 20px;
  cursor: pointer;
  line-height: 36px;
  font-size: 14px;
}
.addResouce {
  width: 100%;
  margin: 20px 20px;
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  margin-left: 30px;
  font-size: 14px;
}
.addControl {
  margin-left: 20px;
  color: #169bd5;
  cursor: pointer;
}
.fileList {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: row nowrap;
  align-items: flex-start;
  font-size: 14px;
  color: #606266;
}
.fileListBox {
  display: flex;
  flex-flow: column nowrap;
  align-items: flex-start;
  color: #169bd5;
  font-size: 14px;
  margin-left: 20px;
  font-weight: 700;
}
.fileListBox > span {
  margin-bottom: 10px;
  font-weight: normal;
}
</style>
